package com.hopu.v1.service.admin.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.mapper.admin.system.RoleMenuMapper;
import com.hopu.v1.pojo.system.Menu;
import com.hopu.v1.pojo.system.RoleMenu;
import com.hopu.v1.service.admin.system.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YwaiX
 * @since 2024-09-12
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    //开启事务
    @Transactional
    @Override
    public int editRoleMenu(int roleId,int[] menuIds) {
        //删除角色菜单列表，通过角色ID
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId,roleId));
        //判断menuIds是否有值，有值进行添加
        if (menuIds != null && menuIds.length > 0) {
            ArrayList<RoleMenu> roleMenus = new ArrayList<>();
            for (int i = 0; i < menuIds.length; i++) {
                roleMenus.add(new RoleMenu(roleId, menuIds[i]));
            }
            roleMenuMapper.insert(roleMenus);
        }
        return 1;
    }
    @Override
    public List<Integer> getRoleMenu(int roleId) {
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId));
        if (roleMenus != null && !roleMenus.isEmpty()) {
            List<Integer> menuIds = new ArrayList<>();
            new ArrayList<>(roleMenus).forEach(roleMenu -> {menuIds.add(roleMenu.getMenuId());});
            return menuIds;
        }else {
            return null;
        }

    }

    @Override
    public List<Menu> getMenuList() {
        return List.of();
    }

    @Override
    public List<Menu> getStaffMenuList(Integer staffId) {
        return roleMenuMapper.selectMenuByUserId(staffId);
    }
}
